package com.kkb.kkbadmin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.kkbadmin.dao.ProjectInfoDao;
import com.kkb.kkbadmin.domain.ProjectInfo;
import com.kkb.kkbadmin.domain.User;
import com.kkb.kkbadmin.service.ProjectManagementService;
import com.kkb.kkbadmin.service.ProjectRegisterService;
import com.kkb.kkbcommon.api.ResultCode;
import com.kkb.kkbcommon.exception.Asserts;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @ClassName ProjectManagementService
 * @Description 项目管理service
 * @Author 侯兴飞
 * @Date 2021/8/13
 **/
//ServiceImpl<RecommendMapper, Recommend>  继承mp内置的service方法 里面包括依赖注入方式中的泛型注入baseMapper
@Service
public class ProjectManagementServiceImpl extends ServiceImpl<ProjectInfoDao, ProjectInfo> implements ProjectManagementService {
    @Resource
    private ProjectRegisterService prService;

    /**
     * 分页查询
     *
     * @param condition 分页条件对象
     * @param pageNum   当前页码
     * @param pageSize  分页查询条数
     * @param user      用户信息
     * @return PageInfo<ProjectInfo>
     */
    @Override
    public PageInfo<ProjectInfo> findPage(ProjectInfo condition, User user, int pageNum, int pageSize) {
        Integer status = condition.getStatus();
        String type = condition.getType();
        Date releaseTime = condition.getReleaseTime();
        PageInfo<ProjectInfo> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            //lambdaquerywrapper的写法
            lambdaQuery().eq(ObjectUtil.isNotNull(status), ProjectInfo::getStatus, status)
                    .eq(ObjectUtil.isNotNull(type), ProjectInfo::getType, type)
                    .lt(ObjectUtil.isNotNull(releaseTime), ProjectInfo::getReleaseTime, releaseTime)
                    .list();
        });
        //todo:统计学生和用户已报名人数
        /*ArrayList<Integer> list1 = new ArrayList<>();
        list.forEach((projectInfo)->{
            Long projectId = projectInfo.getId();
            //统计学生已报名人数
            int stuCount = prService.count(user, projectId, 0);
            //统计导师已报名人数
            int teacherCount = prService.count(user, projectId, 1);
            list1.add(stuCount);
            list1.add(teacherCount);
        });*/
        return pageInfo;
    }

    /**
     * 根据项目id查询已完成的项目
     *
     * @param id
     * @return
     */
    @Override
    public ProjectInfo finished(Long id) {
        ProjectInfo byId = findById(id);
        if (byId.getStatus() != 2) {
            return null;
        }
        return byId;
    }

    /**
     * 添加项目
     *
     * @param info 项目信息
     * @return void
     */
    @Override
    public void add(ProjectInfo info) {
        int i = baseMapper.insert(info);
        //业务在service写，逻辑不能出现在controller层中
        if (i == 0) {
            //在此处抛异常，不返回数据
            Asserts.fail("上传失败，请检查！");
        }
    }

    /**
     * 根据ID查询项目详情
     *
     * @param id 项目id
     * @return CommonResult<ProjectInfo>
     */
    @Override
    public ProjectInfo findById(long id) {
        ProjectInfo projectInfo = baseMapper.selectById(id);
        if (projectInfo == null) {
            Asserts.fail(ResultCode.FAILED);
        }
        return projectInfo;
    }

    /**
     * 根据id修改项目信息
     *
     * @param info 项目信息
     * @param id   项目id
     * @return void
     */
    @Override
    public void update(ProjectInfo info, long id) {
        info.setId(id);
        int i = baseMapper.updateById(info);
        if (i == 0) {
            Asserts.fail(ResultCode.FAILED);
        }
    }

    /**
     * 逻辑删除
     *
     * @param id 项目id
     * @return void
     */
    @Override
    public void delete(long id) {
        int delete = baseMapper.deleteById(id);
        if (delete == 0) {
            Asserts.fail(ResultCode.FAILED);
        }
    }

    /**
     * 修改项目状态
     *
     * @param id     项目id
     * @param status 项目状态码  0报名中、1进行中、2已完成、3已取消
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(id);
        projectInfo.setStatus(status);
        int i = baseMapper.updateById(projectInfo);
        if (i == 0) {
            Asserts.fail("更新失败");
        }
    }

    /**
     * 管理员审核导师 ：修改报名表的状态为已报名，0 未报名，1 审核中，2 已报名
     *
     * @param user      导师
     * @param projectId 项目id
     */
    @Override
    public void auditTeacher(User user, Long projectId) {
        Long id = user.getId();
        //修改报名表的状态为 2已报名
        prService.updateStatus(id, projectId, 2);
        //修改项目表状态为 1进行中
        updateStatus(projectId, 1);
    }

}
